package com.eqdd.yiqidian.helper.mvchelper;


import com.eqdd.yiqidian.http.HttpPresneter;

import java.util.List;

import rx.Observable;

/**
 * Created by lvzhihao on 17-3-30.
 */

public class ModelRxDataSource<T> extends MRxDataSource<List<T>> {
    public static final int PAGESIZE = 10;
    private boolean isMore = false;
    public int mPage = 1;
    public int lastdId = 0;
    HttpPresneter httpPresneter;
    OnComposeListener<T> onComposeListener;
    OnComposeStringListener onComposeStringListener;
    boolean isList=true;

    public void setOnComposeListener(OnComposeListener<T> onComposeListener) {
        this.onComposeListener = onComposeListener;
    }
    public void setOnComposeStringListener(OnComposeStringListener onComposeListener) {
        this.onComposeStringListener = onComposeListener;
    }

    public void setHttpPresneter(HttpPresneter httpPresneter) {
        this.httpPresneter = httpPresneter;
    }
    public void setHttpPresneter(HttpPresneter httpPresneter,boolean isList) {
        this.httpPresneter = httpPresneter;
        this.isList=isList;
    }
    public interface OnComposeListener<T>{
         Observable<List<T>> compose(Observable<List<T>> observable);
    }

    public interface OnComposeStringListener<T,S>{
        Observable<List<T>> compose(Observable<S> observable);
    }


    private Observable<List<T>> load(final int page, DoneActionRegister<List<T>> register) throws Exception {

        if (isList){
            return onComposeListener==null?httpPresneter.postObserverList():onComposeListener.compose(httpPresneter.postObserverList());
        }else {
            return onComposeStringListener.compose(httpPresneter.postObserverBean());
        }
    }

    @Override
    public boolean hasMore() {
        return isMore;
    }

    @Override
    public Observable<List<T>> refreshRXM(DoneActionRegister<List<T>> register) throws Exception {
        mPage = 1;
        return load(mPage, register);
    }

    @Override
    public Observable<List<T>> loadMoreRXM(DoneActionRegister<List<T>> register) throws Exception {
        return load(++mPage, register);
    }

}